<?php

/*
class Pager, helper for paging results of database query
Copyright (C) 2011  Petr Čermák

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

require_once 'DbTable.php';
require_once 'Checker.php';
require_once 'AbstractHelper.php';

class Pager extends AbstractHelper{
    
    private $table;
    private $tableSize;
    private $pageParam;
    private $pageRowCount;

    /**
     * Creates instance of Pager.
     * @param Controller  controller that owns this pager
     * @param DbTable  instance of a table providing data to be paged
     * @param integer  count of rows per page
     * @param string  parameter that will be used for page identification
     */
    public function __construct(Controller $owner, DbTable $table, $pageRowCount, $pageParam = "skip"){
        Checker::checkInteger($pageRowCount);
        $this->pageRowCount = $pageRowCount;
        $this->pageParam = $pageParam;
        $this->table = $table;
        $this->owner = $owner;
        $this->tableSize = $table->getRowCount();
        $owner->reserveParameter($pageParam);
    }

    public function setOwner(Controller $owner){
        parent::setOwner($owner);
        $owner->reserveParameter($pageParam);
    }

    /**
     * Creates an array of all page addresses.
     * @param Request  request associated with owning controller
     * @return array  array of objects with following attributes:
     * addr - the page address,
     * sel - bool if page is selected,
     * num - number of page,
     * lb - lower row bound, 
     * ub - upper row bound
     */
    public function getAddresses(Request $request){
        $addresses = array();
        $offset = 0;
        $size = $this->tableSize;
        $page = $request->getParameter($this->pageParam);
        if($page == null)$page = 0;
        $skip = 0;
        while($offset < $size){
            $lb = $offset + 1;
            $ub = $offset + $this->pageRowCount;
            if($ub > $size)$ub = $size;
            $param = array($this->pageParam => $skip);
            if($skip == 0)$param[$this->pageParam] = null;
            $obj = new stdClass();
            $obj->addr = $this->owner->createReloadAddress($param);
            $obj->sel = $page == $skip;
            $obj->num = $skip;
            $obj->lb = $lb;
            $obj->ub = $ub;
            $addresses[$skip] = $obj;
            /*$addresses[$skip] = array(
                "addr" => $this->owner->createReloadAddress($param),
                "lb" => $lb,
                "ub" => $ub,
                "num" => $skip,
                "sel" => $page == $skip
            );*/
            $offset += $this->pageRowCount;
            $skip++;
        }
        return $addresses;
    }

    /**
     * Creates an array of all page addresses.
     * @param Request  request associated with owning controller
     * @return array  array of rows from table associated with selected page
     */
    public function getRows(Request $request){
        $page = $request->getParameter($this->pageParam);
        if($page == null)$page = 0;
        $offset = $page*$this->pageRowCount;
        return $this->table->fetchLimited($this->pageRowCount, $offset);
    }

}
?>
